# -*- coding:utf-8 -*-
"""
对端口的操作
"""
import os


def findTask(task_name):
    """
    输入任务名称，查询对应的pid
    :param task_name:
    :return:
    """
    cmd = "tasklist |findstr {}".format(task_name)

    result = os.popen(cmd)
    text = result.read()

    tasks = []
    try:

        task_list = text.split('\n')
        for task in task_list:
            s_task = task.split(' ')
            clear_s_task = [i for i in s_task if i != '']
            g_task,pid = clear_s_task[0],clear_s_task[1]
            tasks.append((g_task,pid))

    except Exception as findPid_ERR:
        print(findPid_ERR)

    return tasks



def killTask(pid):
    """
    输入任务pid 停止task
    :param pid:
    :return:
    """

    cmd = "taskkill /f /im {}".format(pid)
    os.system(cmd)